<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="20">
          <a-col :md="5" :sm="10">
            <a-form-item label="账号">
              <a-input placeholder="请输入账号" v-model="queryParam.username"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="10">
            <a-form-item label="姓名">
              <a-input placeholder="请输入姓名" v-model="queryParam.realname"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="5">
            <span style="float: left; overflow: hidden; margin-top: 5px" class="table-page-search-submitButtons">
              <a-button size="small" type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button size="small" type="primary" @click="searchReset" icon="reload" style="margin-left: 8px"
                >重置</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator" style="border-top: 5px">
      <a-button size="small" @click="handleAdd" v-has="'user:add'" type="primary" icon="plus">添加用户</a-button>
      <a-button size="small" @click="handleSyncUser" type="primary" v-has="'user:doSyncUser'" icon="file-sync"
        >同步流程</a-button
      >
      <a-button
        size="small"
        type="primary"
        icon="download"
        @click="handleExportXls('用户信息')"
        v-has="'user:exportXls'"
        >导出</a-button
      >
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        bordered
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="roleName" slot-scope="text">
          <j-ellipsis :value="text" :length="15"></j-ellipsis>
        </template>

        <template slot="avatarslot" slot-scope="text, record">
          <div class="anty-img-wrap">
            <div id="avatarBox">
              <a-avatar shape="square" :src="getAvatarView(record.avatar)" icon="user" />
            </div>
          </div>
        </template>
        <template slot="email" slot-scope="text">
          <j-ellipsis :value="text" :length="18"></j-ellipsis>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-has="'user:edit'" style="margin-right: 5px">编辑</a>
          <a href="javascript:;" @click="handleDetail(record)" v-has="'user:detail'">详情</a>

          <a
            href="javascript:;"
            style="margin-left: 5px"
            v-has="'user:resetpassword'"
            @click="handleChangePassword(record.username)"
            >密码</a
          >

          <a-popconfirm
            title="确定冻结吗?"
            v-if="record.status == 1"
            v-has="'user:freezeuser'"
            @confirm="() => handleFrozen(record.id, 2)"
          >
            <a style="margin-left: 5px">冻结</a>
          </a-popconfirm>

          <a-popconfirm
            title="确定解冻吗?"
            style="margin-left: 5px"
            v-if="record.status == 2"
            @confirm="() => handleFrozen(record.id, 1)"
          >
            <a>解冻</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <user-modal ref="modalForm" @ok="modalFormOk"></user-modal>

    <password-modal ref="passwordmodal" @ok="passwordModalOk"></password-modal>

    <sys-user-agent-modal ref="sysUserAgentModal"></sys-user-agent-modal>
  </a-card>
</template>

<script>
import JDate from '@/components/jeecg/JDate.vue'
import UserModal from './modules/UserModal'
import PasswordModal from './modules/PasswordModal'
import { putAction } from '@/api/manage'
import { frozenBatch } from '@/api/api'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import SysUserAgentModal from './modules/SysUserAgentModal'
import moment from 'moment'
import JEllipsis from '../../components/jeecg/JEllipsis'
export default {
  name: 'UserList',
  mixins: [JeecgListMixin],
  components: {
    JEllipsis,
    SysUserAgentModal,
    UserModal,
    PasswordModal,
    JDate,
    moment,
  },
  data() {
    return {
      entryTime: '',
      description: '这是用户管理页面',
      queryParam: {},
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 50,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '用户账号',
          align: 'center',
          dataIndex: 'username',
          width: 80,
        },
        {
          title: '姓名',
          align: 'center',
          width: 80,
          dataIndex: 'realname',
        },
        {
          title: '头像',
          align: 'center',
          width: 50,
          dataIndex: 'avatar',
          scopedSlots: { customRender: 'avatarslot' },
        },
        {
          title: '性别',
          align: 'center',
          width: 50,
          dataIndex: 'sex_dictText',
        },
        {
          title: '生日',
          align: 'center',
          width: 70,
          dataIndex: 'formatBirthday',
        },
        {
          title: '手机号码',
          align: 'center',
          width: 110,
          dataIndex: 'phone',
        },
        {
          title: '角色',
          align: 'center',
          dataIndex: 'roleName',
          scopedSlots: { customRender: 'roleName' },
          width: 120,
        },
        {
          title: '所属部门',
          align: 'center',
          dataIndex: 'departName',
          width: 120,
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center',
          width: 170,
        },
      ],
      url: {
        imgerver: window._CONFIG['domianURL'] + '/sys/common/view',
        syncUser: '/process/extActProcess/doSyncUser',
        list: '/sys/user/list',
        delete: '/sys/user/delete',
        deleteBatch: '/sys/user/deleteBatch',
        exportXlsUrl: '/sys/user/exportXls',
        importExcelUrl: 'sys/user/importExcel',
      },
      /* 排序参数 */
      isorter: {
        column: '',
        order: '',
      },
      dakaRange: [],
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    moment,
    updateIDCard(record) {
      console.log(record)
      console.log(this.$refs.updateIdcard)
      this.$refs.updateIdcard.open(record)
    },
    search() {
      if (this.entryTime) {
        this.queryParam.entryTime = this.entryTime.format('YYYY-MM-DD')
        this.searchQuery()
      } else {
        this.queryParam.entryTime = ''
        this.searchQuery()
      }
    },
    getAvatarView: function (avatar) {
      return this.url.imgerver + '/' + avatar
    },

    batchFrozen: function (status) {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return false
      } else {
        let ids = ''
        let that = this
        that.selectedRowKeys.forEach(function (val) {
          ids += val + ','
        })
        that.$confirm({
          title: '确认操作',
          content: '是否' + (status == 1 ? '解冻' : '冻结') + '选中账号?',
          onOk: function () {
            frozenBatch({ ids: ids, status: status }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          },
        })
      }
    },
    handleMenuClick(e) {
      if (e.key == 1) {
        this.batchDel()
      } else if (e.key == 2) {
        this.batchFrozen(2)
      } else if (e.key == 3) {
        this.batchFrozen(1)
      }
    },
    handleFrozen: function (id, status) {
      let that = this
      frozenBatch({ ids: id, status: status }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    handleChangePassword(username) {
      this.$refs.passwordmodal.show(username)
    },
    handleAgentSettings(username) {
      this.$refs.sysUserAgentModal.agentSettings(username)
      this.$refs.sysUserAgentModal.title = '用户代理人设置'
    },
    //同步流程
    handleSyncUser() {
      var that = this
      putAction(that.url.syncUser, {}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    passwordModalOk() {
      //TODO 密码修改完成 不需要刷新页面，可以把datasource中的数据更新一下
    },
    searchReset() {
      this.queryParam = {}
      this.dakaRange = []
      this.loadData()
    },
    selectDate(date, dateString) {
      this.queryParam.queryEntryStartTime = dateString[0]
      this.queryParam.queryEntryEndTime = dateString[1]
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
<style>
#avatarBox .ant-avatar {
  margin-top: -3px;
}
</style>